package com.xls.commonlibrary.util;

import static com.xls.commonlibrary.constant.Const.GET_EDIT_LIST;
import static com.xls.commonlibrary.constant.Const.GET_PLAY_LIST;

import android.text.TextUtils;

import com.orhanobut.logger.Logger;
import com.xls.commonlibrary.http.SetUtil;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Nonnull;

public class AdSetCacheUtil {
    private static volatile AdSetCacheUtil singleton = null;

    private AdSetCacheUtil() {
    }

    public static AdSetCacheUtil getInstance() {
        if (singleton == null) {
            synchronized (AdSetCacheUtil.class) {
                if (singleton == null) {
                    singleton = new AdSetCacheUtil();
                }
            }
        }
        return singleton;
    }

    // todo 退出登录需要清空缓存

    // 播放列表
    public void savePlayList(@Nonnull List<String> list) {
        if (list.size() > 0) {
            MMKVUtil.getInstance().getKV().encode(GET_PLAY_LIST, ListStringUtil.list2String(list));
        } else {
            // 清空
            MMKVUtil.getInstance().getKV().encode(GET_PLAY_LIST, "");
        }
    }

    public List<String> getPlayList() {
         String list = MMKVUtil.getInstance().getKV().decodeString(GET_PLAY_LIST);
        if (TextUtils.isEmpty(list)) {
            return new ArrayList<>();
        } else {
            return ListStringUtil.string2List(list);
        }

    }


    // ecpm填补列表
    public void saveEditList(@Nonnull List<String> list) {
        if (list.size() > 0) {
            // todo  mmkv 保存list，需要先转为set
            Set<String> set = SetUtil.listToSet(list);

            MMKVUtil.getInstance().getKV().encode(GET_EDIT_LIST, set);
        } else {
            // 清空
            MMKVUtil.getInstance().getKV().encode(GET_EDIT_LIST, new HashSet<>());
        }
    }

    public Set<String> getEditList() {
        // test
        Set<String> setGet = MMKVUtil.getInstance().getKV().decodeStringSet(GET_EDIT_LIST);
        Logger.d("=========getEditList======:" + setGet);

        return setGet;
    }

}
